﻿namespace EyeOpen.Mapping
{
    using System;
    using System.Collections.Generic;
    using System.Reflection;

    public class PropertyComparer : IComparer<PropertyInfo>
    {
        public int Compare(PropertyInfo x, PropertyInfo y)
        {
            if (x.PropertyType.Equals(y.PropertyType)) 
            {
                return x.Name.CompareTo(y.Name);
            }

            if ((BaseMapper.IsBaseType(x.PropertyType)) && (BaseMapper.IsBaseType(y.PropertyType)))
            {
                return x.Name.CompareTo(y.Name);
            }

            if ((!BaseMapper.IsBaseType(x.PropertyType)) && (BaseMapper.IsBaseType(y.PropertyType)))
            {
                return 1;
            }

            if ((BaseMapper.IsBaseType(x.PropertyType)) && (!BaseMapper.IsBaseType(y.PropertyType)))
            {
                return -1;
            }
            
            if ((!BaseMapper.IsBaseType(x.PropertyType)) && (!BaseMapper.IsBaseType(y.PropertyType)))
            {
                return x.Name.CompareTo(y.Name);
            }

            throw new Exception();
        }
    }
}
